import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/01-login.vue'
import layout from '@/views/0-layout.vue'
import search from '@/views/03-search.vue'
import homepage from '@/views/home-page.vue'
import ask from '@/views/0-ask.vue'
import my from '@/views/0-my.vue'
import collectiom from '@/views/0-Collection'
import mylease from '@/views/my-lease'
import citylist from '@/views/city-list'
import releasehouse from '@/views/release-house'
import { getToken } from '@/utils/storage'
import searchxiaoqu from '@/views/search-xiaoqu'
import map from '@/views/map-map'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      redirect: '/home-page',
      component: layout,
      children: [
        {
          path: '/home-page',
          component: homepage
        },
        {
          path: '/03-search',
          component: search
        },
        {
          path: '/0-my',
          component: my
        },
        {
          path: '/0-ask',
          component: ask
        }

      ]
    },
    {
      path: '/0-Collection',
      component: collectiom
    },
    {
      path: '/my-lease',
      component: mylease
    },
    {
      path: '/login',
      component: login
    },
    {
      path: '/city-list',
      component: citylist
    },
    {
      path: '/release-house',
      component: releasehouse
    },
    {
      path: '/search-xiaoqu',
      component: searchxiaoqu
    },
    {
      path: '/map-map',
      component: map
    }

    // {
    //   path: '/login',
    //   component: login
    // }
  ]
})

// 路由前置守卫
const whileList = ['/login']// 白名单
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token) {
    next()
  } else {
    if (whileList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
